package com.zhongyouyun.admin.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.*;
import com.zhongyouyun.admin.mapper.RoleMapper;
import com.zhongyouyun.admin.pojo.DictPojo;
import com.zhongyouyun.admin.pojo.RolePojo;
import com.zhongyouyun.admin.service.RoleDictService;
import com.zhongyouyun.admin.service.RoleMenuService;
import com.zhongyouyun.admin.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyouyun.utils.ModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: shenyi
 * @Date: 2021-09-23
 * @Description: 角色表服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleModel> implements RoleService {
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private RoleDictService roleDictService;

    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 分页查询
     * @param page 分页数据  lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public IPage<RoleModel> selectPage(Page<RoleModel> page, QueryWrapper<RoleModel> queryWrapper) {
        return this.baseMapper.selectPage(page, queryWrapper);
    }


    /*

     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 查询全部
     * @param   lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public List<RoleModel> selectList(QueryWrapper<RoleModel> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<MenuModel> findRoleMenus(RoleModel roleModel) {

        List<MenuModel> roleMenus = this.baseMapper.findRoleMenus(roleModel);
        List<DictModel> dictModels = this.baseMapper.findRoleDicts(roleModel);
        List<MenuModel> newMenus = new ArrayList<MenuModel>();
        roleMenus.forEach(menuModel -> {
            dictModels.forEach(dictModel -> {
                if (menuModel.getId().equals(dictModel.getMenuId())) {
                    MenuModel model = new MenuModel();
                    model.setName(dictModel.getDescription());
                    model.setId(dictModel.getId());
                    model.setpId(menuModel.getId());
                    model.setType("2");
                    newMenus.add(model);
                }
            });
        });
        roleMenus.addAll(newMenus);
        return roleMenus;
    }

    @Override
    @Transactional
    public Boolean saveRoleMenus(UserModel model, List<DictModel> list) {
        Boolean flag = true;
        if (list.size() > 0) {
            Long id = list.get(0).getId();
            QueryWrapper<RoleDictModel> roleDictModelQueryWrapper = new QueryWrapper();
            roleDictModelQueryWrapper.eq("role_id" , id);
            roleDictService.remove(roleDictModelQueryWrapper);
            QueryWrapper<RoleMenuModel> roleMenuModelQueryWrapper = new QueryWrapper();
            roleMenuModelQueryWrapper.eq("role_id" , id);
            roleMenuService.remove(roleMenuModelQueryWrapper);
            List<RoleDictModel> roleDictModelList = new ArrayList<RoleDictModel>();
            List<RoleMenuModel> roleMenuModelList = new ArrayList<RoleMenuModel>();

            list.forEach(dictModel -> {
                String type = dictModel.getType();
                if ("3".equals(type)) {
                    RoleDictModel roleDictModel = new RoleDictModel();
                    ModelUtils modelutils = new ModelUtils();
                    modelutils.initModel(roleDictModel, model.getId().toString());
                    roleDictModel.setRoleId(id);
                    roleDictModel.setMenuId(Integer.valueOf(dictModel.getRemarks()));
                    roleDictModel.setDictId(dictModel.getMenuId());
                    roleDictModelList.add(roleDictModel);
                } else {
                    RoleMenuModel roleMenuModel = new RoleMenuModel();
                    ModelUtils modelutils = new ModelUtils();
                    modelutils.initModel(roleMenuModel, model.getId().toString());
                    roleMenuModel.setRoleId(id);
                    roleMenuModel.setMenuId(dictModel.getMenuId());
                    roleMenuModelList.add(roleMenuModel);
                }
            });
            flag = roleDictService.saveOrUpdateBatch(roleDictModelList);
            flag = roleMenuService.saveOrUpdateBatch(roleMenuModelList);
        }
        return flag;
    }

    @Override
    public List<RoleModel> findByUserId(Long id) {
        return this.baseMapper.findByUserId(id);
    }

    @Override
    public List<Map<String, Object>> findRoles() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<RoleModel> list = this.baseMapper.selectList(queryWrapper);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        list.forEach(roleModel -> {
            Map map = new HashMap();
            map.put("value" , roleModel.getId() + "");
            map.put("label" , roleModel.getName());
            mapList.add(map);
        });
        return mapList;
    }
    public IPage<RolePojo> selectPojoPage(Page page, QueryWrapper<RolePojo> queryWrapper) {
        return this.baseMapper.selectPojoPage(page, queryWrapper);
    }

    @Override
    public List<RoleModel> selectbyList(List<Long> longva) {
        return this.baseMapper.selectbyList(longva);
    }

}
